package com.zhangkunhao.notes.web.servlet;

import com.alibaba.fastjson.JSON;
import com.zhangkunhao.notes.entity.Do.AnswerComment;
import com.zhangkunhao.notes.entity.bo.Message;
import com.zhangkunhao.notes.entity.bo.PageBean;
import com.zhangkunhao.notes.service.AnswerCommentServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;

/**
 * 评论的controller
 *
 * @author 20642
 */
@WebServlet("/answerComment/*")
public class AnswerCommentServlet extends BaseDaoServlet {
    private AnswerCommentServiceImpl commentService = new AnswerCommentServiceImpl();

    public void publishComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.接收数据
        String params = request.getReader().readLine();

        //2.转为对象
        AnswerComment answerComment = JSON.parseObject(params, AnswerComment.class);
        System.out.println("控制层的answerComment：" + answerComment);

        //3.调用service层
        Message<AnswerComment> commentMessage = commentService.publishComment(answerComment);
        System.out.println("commentMessage:" + commentMessage);

        //4.转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(commentMessage));
    }

    public void reportComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //接收数据乱码解决
        int reportCommentId = Integer.parseInt(new String(request.getParameter("reportCommentId").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
        String reportCommentReason = new String(request.getParameter("reportCommentReason").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);

        //调用service层
        Message<AnswerComment> reportQuestionMessage = commentService.reportComment(reportCommentId, reportCommentReason);

        // 转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(reportQuestionMessage));
    }

    public void listReportedComments(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.调用service层,封装返回前端的对象
        PageBean<AnswerComment> commentPageBean = commentService.listReportedComments();

        //2.转换为json数据，写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(commentPageBean));
    }

    public void agreedReportComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.接收数据乱码解决
        int commentId = Integer.parseInt(new String(request.getParameter("agreedReportCommentId").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));

        //2.调用service层
        Message<AnswerComment> commentMessage = commentService.agreedReportComment(commentId);

        //3.转换为json数据，写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(commentMessage));
    }

    public void disagreedReportComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.接收数据乱码解决
        int commentId = Integer.parseInt(new String(request.getParameter("disagreedReportCommentId").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));

        //2.调用service层
        Message<AnswerComment> commentMessage = commentService.disagreedReportComment(commentId);

        //3.转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(commentMessage));
    }
}
